package com.example.yuanboot.common.util;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.SneakyThrows;

import java.util.Arrays;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
 * 复杂任务处理工具类
 *
 * @author qingyan 2021-06-29
 */
public class ComplexTaskUtils {

    static ThreadPoolExecutor executorService = new ThreadPoolExecutor(10, 10, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000), new ThreadFactoryBuilder().setNameFormat("线程池-%s").build());

    @SneakyThrows
    public static Object[] process(Function function, Object... objs) {
        final Object[] result = executorService.submit(() -> {
            return Arrays.asList(objs).parallelStream().map(function)
                    .toArray();
        }).get();
        return result;
    }


}
